/**
 * 
 */
package com.p2p.front.component.impl;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.p2p.base.bizInterface.ChangeNotify;
import com.p2p.base.model.Money;
import com.p2p.base.util.ChangeNotifyUtil;
import com.p2p.base.util.DoubleUtil;
import com.p2p.front.component.TenderPointSetting;
import com.p2p.front.dal.model.SysConfig;
import com.p2p.front.service.SysConfigService;

/**
 * @author ruiqi
 *
 */
@Component("tenderPointSetting")
public class TenderPointSettingImpl implements TenderPointSetting,ChangeNotify {

	private final Logger logger = LoggerFactory.getLogger(TenderPointSettingImpl.class);
	
	private double tenderPointProduceRate= 1l;
	
	@Resource
	private SysConfigService sysConfigService;
	
	@PostConstruct
	@Override
	public void addListener() {
		ChangeNotifyUtil.addListener(SysConfigService.notifySubject, this);
	}

	@Override
	public void change() {
		logger.info("initTenderPointSetting");
		this.initTenderPointSetting();
	}
	@PostConstruct
	public void initTenderPointSetting(){
		SysConfig sysConfig = this.sysConfigService.getByKey(SysConfig.ConfigKeyEnum.TenderPointProduce);
		if(sysConfig==null){
			throw new RuntimeException("sys config "+SysConfig.ConfigKeyEnum.TenderPointProduce.name()+" is null ");
		}
		String value = sysConfig.getValue();
		tenderPointProduceRate = Double.valueOf(value);
	}

	
	@Override
	public int calcuTenderPoint(Money tenderAmount,int deadLineNum) {
		//聚福鑫特殊逻辑，积分不看投标期限，期限永远是1，暂时硬编码
		if(deadLineNum>1){
			deadLineNum=1;
		}
		return new Double(DoubleUtil.mul(tenderAmount.doubleValueYuan(), deadLineNum)*tenderPointProduceRate).intValue();
	}

}
